<div [@routerTransition] (click)="clickContainer()">
    <div class="content d-flex flex-column flex-column-fluid">
        <sub-header [title]="'Device' | localize" [description]="'DeviceManage' | localize">
            <div role="actions">
                <div class="btn-group dropdown" dropdown
                    *ngIf="isGranted('Pages.Tenant.Devices.Apply')&&appSession.tenant">
                    <button class="btn btn-primary dropdown-toggle icon-piliangcaozuo" type="button" id="applyButton"
                        data-toggle="dropdown" dropdownToggle aria-haspopup="true" aria-expanded="false">
                        {{l('batchOperation')}}
                    </button>
                    <div class="dropdown-menu" *dropdownMenu aria-labelledby="applyButton" x-placement="bottom-start">
                        <a class="dropdown-item icon-shangxian1" (click)="online()">
                            {{l('onlineBatch')}}
                        </a>
                        <a class="dropdown-item icon-xiaxian1" (click)="offline()">
                            {{l('offlineBatch')}}
                        </a>
                        <div class="dropdown-divider"></div>
                        <a class="dropdown-item icon-shuminghaoshang_xian" (click)="onlineAll()">
                            {{l('onlineAll')}}
                        </a>
                        <a class="dropdown-item icon-shuminghaoxia_xian" (click)="offlineAll()">
                            {{l('offlineAll')}}
                        </a>
                        <a class="dropdown-item icon-shanchu" (click)="deleteBatch()">
                            {{l('deleteBatch')}}
                        </a>
                    </div>
                </div>

                <div class="btn-group dropdown" dropdown
                    *ngIf="isGranted('Pages.Tenant.Devices.Publish')&&appSession.tenant">
                    <button class="btn btn-primary dropdown-toggle icon-fabu" type="button" id="publishButton"
                        data-toggle="dropdown" dropdownToggle aria-haspopup="true" aria-expanded="false">
                        {{l('publishDevice')}}
                    </button>
                    <div class="dropdown-menu" *dropdownMenu aria-labelledby="publishButton" x-placement="bottom-start">
                        <a class="dropdown-item icon-fabu1" (click)="goPublishDevice()">
                            {{l('publishDevice')}}
                        </a>
                        <a class="dropdown-item icon-fabu1" (click)="goPublishMirror()">
                            {{l('mirrorPublish')}}
                        </a>
                        <a class="dropdown-item icon-chehui" (click)="withdrawDevice()">
                            {{l('withdrawDevice')}}
                        </a>
                    </div>
                </div>


                <div class="btn-group dropdown" dropdown *ngIf="isGranted('Pages.Tenant.Devices.Create')">
                    <button class="btn btn-primary dropdown-toggle icon-excel" type="button" id="port"
                        data-toggle="dropdown" dropdownToggle aria-haspopup="true" aria-expanded="false"
                        [buttonBusy]="exportLoading">
                        {{l('ImportOrExport')}}
                    </button>
                    <div class="dropdown-menu" *dropdownMenu aria-labelledby="port" x-placement="bottom-start">
                        <a class="dropdown-item icon-shuminghaoxia_xian" (click)="goImport()">
                            {{l('import')}}
                        </a>
                        <a class="dropdown-item icon-shuminghaoshang_xian" (click)="goExport()">
                            {{l('export')}}
                        </a>
                    </div>
                </div>

                <button class="btn btn-primary" (click)="createDevice()"
                    *ngIf="isGranted('Pages.Tenant.Devices.Create')&&appSession.tenant">
                    <i class="icon-add"></i>
                    {{l("CreateNewDevice")}}</button>
            </div>
        </sub-header>

        <div [class]="containerClass">
            <div class="card card-custom gutter-b">
                <div class="card-body">
                    <form class="form" autocomplete="new-password">
                        <div>
                            <div class="row align-items-center mb-4">
                                <div class="col-2">
                                    <div class="form-group align-items-center">
                                        <label>{{l('Mac')}} / {{l('Name')}} / {{l('outId')}}</label>
                                        <div class="input-group">
                                            <input [(ngModel)]="filterText" name="filterText"
                                                (keyup.enter)="getDevices()" autoFocus class="form-control m-input"
                                                [placeholder]="l('SearchWithThreeDot')" type="text">
                                        </div>
                                    </div>
                                </div>
                                <div class="col-2">
                                    <div class="form-group align-items-center">
                                        <label>&nbsp;</label>

                                        <div class="input-group">
                                            <select class="form-control" [(ngModel)]="status" name="status">
                                                <option value="">
                                                    {{l("All")+' / '+l("Online")+' / '+l("Offline")}}
                                                </option>
                                                <option [value]="0">{{l("Offline")}}</option>
                                                <option [value]="1">{{l("Online")}}</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>
                                <div class="col-2" *ngIf="!appSession.tenant">
                                    <div class="form-group align-items-center">
                                        <label>&nbsp;</label>

                                        <div class="input-group">
                                            <select class="form-control" [(ngModel)]="tenantId" name="tenantId"
                                                [disabled]="!tenants">
                                                <option value="">{{l("AllTenants")}}</option>
                                                <option *ngFor="let tenant of tenants" [value]="tenant.id">
                                                    {{tenant.name+'('+tenant.tenancyName+')'}}</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>

                                <div class="col-2">
                                    <div class="form-group align-items-center">
                                        <label>&nbsp;</label>

                                        <div class="input-group">
                                            <select class="form-control" [(ngModel)]="auditStatus" name="auditStatus">
                                                <option value="">{{l("All")+' / '+l("On")+' / '+l("Off")}}
                                                </option>
                                                <option [value]="AuditStatus2.Offline">{{l("Off")}}</option>
                                                <option [value]="AuditStatus2.Online">{{l("On")}}</option>
                                            </select>
                                        </div>
                                    </div>
                                </div>


                                <!-- 按设备类型筛选 -->
                                <div class="col-2 form-group">
                                    <app-high-tree #highTree1 [treeList]="deviceTypeList"
                                        [selfConfig]="{labelName:l('DeviceType')}" [config]="{name:'name'}"
                                        (onTreeUpdate)="onTreeUpdate($event)"></app-high-tree>
                                </div>

                                <div class="col-2 form-group" *ngIf="appSession.tenant">
                                    <app-high-tree #highTree2 [treeList]="deviceTree"
                                        [selfConfig]="{labelName:l('belongStore')}"
                                        (onTreeUpdate)="onTreeUpdate2($event)"></app-high-tree>
                                </div>

                                <div class="col-2">
                                    <div class="form-group align-items-center">
                                        <label style="display:block;">&nbsp;</label>
                                        <div class="input-group">
                                            <button type="button" class="btn btn-primary" (click)="getDevices()">
                                                <i class="la la-refresh"></i>
                                                {{l("search")}}
                                            </button>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </form>

                    
                    <div class="row align-items-center">
                        <!--<Primeng-Datatable-Start>-->
                        <div class="col-12 primeng-datatable-container" [busyIf]="primengTableHelper.isLoading">
                            <!-- resizableColumns="primengTableHelper.resizableColumns" -->
                            <p-table #dataTable (onLazyLoad)="getDevices($event)" [value]="primengTableHelper.records"
                                [rows]="primengTableHelper.defaultRecordsCountPerPage" [paginator]="false" [lazy]="true"
                                [(selection)]="devicePublishList"
                                [resizableColumns]="primengTableHelper.resizableColumns"
                                [responsive]="primengTableHelper.isResponsive">
                                <ng-template pTemplate="header">
                                    <tr>
                                        <th>{{l('Actions')}}</th>
                                        <th style="width: 5em">
                                            <p-tableHeaderCheckbox></p-tableHeaderCheckbox>
                                        </th>
                                        <th style="width:4%">{{l('RecordId')}}</th>
                                        <th style="width:25%">{{l('Name')}}</th>
                                        <th>{{l('type')}}</th>
                                        <th>{{l('Mac')}}</th>
                                        <th pSortableColumn="heartBeatTime" style="width:10%">
                                            {{l('HeartBeatTime')}}
                                            <p-sortIcon field="heartBeatTime"></p-sortIcon>
                                        </th>
                                        <th>{{l('belongStore')}}</th>
                                        <th pSortableColumn="auditStatus">{{l('reviewStatus')}}
                                            <p-sortIcon field="auditStatus"></p-sortIcon>
                                        </th>
                                        <th pSortableColumn="status">{{l('RunningStatus')}}
                                            <p-sortIcon field="status"></p-sortIcon>
                                        </th>
                                        <th *ngIf="!appSession.tenant" pSortableColumn="isLocked">
                                            {{l('IsTest')}}
                                            <p-sortIcon field="isLocked"></p-sortIcon>
                                        </th>
                                    </tr>
                                </ng-template>
                                <ng-template pTemplate="body" let-record let-i="rowIndex">
                                    <tr [pSelectableRow]="record" [attr.trid]="record.id">
                                        <td>
                                            <div class="btn-group dropdown" dropdown  container="body">
                                                <button class="dropdown-toggle btn btn-sm btn-primary"
                                                    dropdownToggle >
                                                    <i class="fa fa-cog"></i>
                                                    <span class="caret"></span> {{l("Actions")}}
                                                </button>
                                                <ul class="dropdown-menu" *dropdownMenu>
                                                    <li *ngIf="isGranted('Pages.Tenant.Devices.Edit')">
                                                        <a (click)="editDevice(record)"
                                                            class="icon-bianji dropdown-item">{{l('Edit')}}</a>
                                                    </li>
                                                    <li
                                                        *ngIf="isGranted('Pages.Tenant.Devices.Delete')&&record.auditStatus==0">
                                                        <a (click)="deleteDevice(record)"
                                                            class="icon-icon-test dropdown-item">{{l('Delete')}}</a>
                                                    </li>
                                                </ul>
                                            </div>
                                        </td>
                                        <td>
                                            <p-tableCheckbox [value]="record"></p-tableCheckbox>
                                        </td>
                                        <td> {{transIndex(i)}}</td>
                                        <td>
                                            <span>{{record.name}}</span>
                                        </td>
                                        <td>
                                            <span *ngIf="record.deviceType">{{record.deviceType.name}}</span>
                                            <span *ngIf="!record.deviceType">-</span>
                                        </td>
                                        <td>
                                            <span>{{record.mac}}</span>
                                        </td>
                                        <td>
                                            <span>{{record.heartBeatTime | date : "yyyy/MM/dd HH:mm"}}
                                            </span>
                                        </td>
                                        <td>
                                            <span *ngIf="!record.storeId"
                                                class="text-warning">{{l('Unassigned')}}</span>
                                            <span *ngIf="record.storeId">{{record.storeName}}</span>
                                        </td>
                                        <td>
                                            <span *ngIf="record['auditStatus']==1"
                                                class="label label-success label-inline">{{l("On")}}</span>
                                            <span *ngIf="record['auditStatus']==0"
                                                class="label label-dark label-inline">{{l("Off")}}</span>
                                        </td>
                                        <td>
                                            <span *ngIf="record['status']==1"
                                                class="label label-success label-inline">{{l("Online")}}</span>
                                            <span *ngIf="record['status']==0"
                                                class="label label-dark label-inline">{{l("Offline")}}</span>
                                        </td>
                                        <td *ngIf="!appSession.tenant">
                                            <span *ngIf="record['operatingType']==0"
                                                class="label label-success label-inline">{{l("Official")}}</span>
                                            <span *ngIf="record['operatingType']==1"
                                                class="label label-dark label-inline">{{l("Test")}}</span>
                                        </td>
                                    </tr>
                                </ng-template>
                                <ng-template pTemplate="emptymessage" let-records>
                                    <tr *ngIf="primengTableHelper.records">
                                        <td colspan="8" class="text-center">
                                            <img class="emptymessage"
                                                src="/assets/common/images/placeholder/deviceHolder.png" />
                                        </td>
                                    </tr>
                                </ng-template>
                            </p-table>
                            <!-- <div class="primeng-no-data" *ngIf="primengTableHelper.totalRecordsCount == 0">
                                {{'NoData' | localize}}
                            </div> -->
                            <div class="primeng-paging-container">
                                <p-paginator [rows]="primengTableHelper.defaultRecordsCountPerPage" #paginator
                                    (onPageChange)="getDevices($event)"
                                    [totalRecords]="primengTableHelper.totalRecordsCount"
                                    [rowsPerPageOptions]="primengTableHelper.predefinedRecordsCountPerPage">

                                </p-paginator>
                                <span class="total-records-count">
                                    {{'TotalRecordsCount' | localize:primengTableHelper.totalRecordsCount}}
                                </span>
                            </div>
                        </div>
                    </div>

                </div>
            </div>
            <div *ngIf="isGranted('Pages.Tenant.Devices.Apply')">
                <div class="modal fade" id="review" tabindex="-1" role="dialog" aria-labelledby="reviewLabel"
                    aria-hidden="true">
                    <div class="modal-dialog modal-sm" role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title" id="reviewLabel">{{l('reviewRequest')}}</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close"
                                    (click)="no()">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <form>
                                    <div class="form-group">
                                        <label for="reason" class="col-form-label">{{l('Message')}}:</label>
                                        <textarea class="form-control" id="reason" name="reason"
                                            [(ngModel)]="apply.reason"></textarea>
                                    </div>
                                </form>
                            </div>
                            <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" (click)="no()"
                                    data-dismiss="modal">{{l('Close')}}</button>
                                <button type="button" class="btn btn-primary" (click)="ok()" [buttonBusy]="busy"
                                    [busyText]="l('SavingWithThreeDot')" [disabled]="busy"><i></i>{{l('Send')}}</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <createOrEditDeviceModal #createOrEditDeviceModal (modalSave)="getDevices()"></createOrEditDeviceModal>
        </div>

    </div>